package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.R;
import com.hermit.qmst.UI.XlistView.XListView;
import com.hermit.qmst.adapter.SystemMessageAdapter;
import com.hermit.qmst.mode.BusinessInfo;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2017/1/5 0005.
 */
public class MessageActivity extends Activity implements XListView.IXListViewListener, View.OnClickListener {
    private String TAG = "消息:MessageActivity";
    private EditText et_search;
    private TextView tv_sreach;
    private TextView tv_hint;
    private XListView mListView;
    private int indexPage = 0;
    private SystemMessageAdapter adapter;
    private ProgressDialog mProgressDialog;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_list);
        title = (TextView) findViewById(R.id.tv_title);
        findViewById(R.id.search).setVisibility(View.GONE);
        title.setText("系统消息");
        findViewById(R.id.iv_back).setOnClickListener(this);
        et_search = (EditText) findViewById(R.id.et_search);
        tv_sreach = (TextView) findViewById(R.id.tv_sreach);
        tv_sreach.setOnClickListener(this);
        tv_hint = (TextView) findViewById(R.id.tv_hint);
        mListView = (XListView) findViewById(R.id.xListView);
        adapter = new SystemMessageAdapter(MessageActivity.this);
        mListView.setAdapter(adapter);
        mListView.setXListViewListener(this);
        mListView.setPullLoadEnable(false);

        initView();
        getData();
    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }

    @Override
    public void onLoadMore() {
        getData();
        indexPage++;
    }

    @Override
    public void onRefresh() {
        adapter.data.clear();
        indexPage = 0;
        getData();
    }

    private void initView() {
        et_search.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 获得焦点
                    tv_hint.setVisibility(View.GONE);
                    tv_sreach.setVisibility(View.VISIBLE);
                } else {
                    // 失去焦点
                    tv_hint.setVisibility(View.VISIBLE);
                    tv_sreach.setVisibility(View.GONE);
                }
            }


        });
    }

    private void getData() {
        mProgressDialog = ProgressDialog.show(this, "提示", "系统正在处理，请稍后…", false, false);
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String searName = "";
        try {
            searName = URLEncoder.encode(et_search.getText().toString().trim(), "utf-8");
        } catch (UnsupportedEncodingException e) {

        }
        String url = Common.mUrl + "manage/qmgj/userMessage.php?type=query&acctid="+ Common.mAgentId + "&agent_id_third=" + Common.mAgentThirdId + "&size="+Common.indexCount+"&curPage=" + indexPage;
        Log.i(TAG + "url", url, true);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                if (object != null && object.length() > 0) {
                    try {
                        Log.i(TAG + "JsonObjectRequest", object.toString(), true);
                        onLoad();
                        if (object.getBoolean("state")) {
                            BusinessInfo info = new BusinessInfo();
                            JSONArray jsonArray = object.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object1 = jsonArray.getJSONObject(i);
                                Log.i(TAG + "JsonObjectRequest", object1.getString("message"), true);
                                info.setId(object1.getInt("acctId"));
                                info.setTitle(object1.getString("title"));
                                String message=object1.getString("message");
                                info.setAddress(message);
                                info.setImg(object1.getJSONObject("createdate").getString("sec"));
                                adapter.data.add(info);
                            }
                            if (jsonArray.length() > Common.indexCount) {
                                mListView.setPullLoadEnable(true);
                            }
                        }
                        adapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                mProgressDialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mProgressDialog.dismiss();
            }
        });
        requestQueue.add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_sreach:
                adapter.data.clear();
                indexPage = 0;
                getData();
                break;
        }
    }
}